﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Wsq.Utils.DoNetFramework40.Socket.tools
{
    class BufferManage
    {
        List<byte[]> _listBuffer = new List<byte[]>();

        public void AddBuffer(byte[] buffer)
        {
            _listBuffer.Add(buffer);
        }

        public bool FindBuffer(byte[] destBuffer, out int index)
        {
            index = -1;
            int flagIndex = 0;

            int count = 0;
            foreach (byte[] buffer in _listBuffer)
            {
                foreach (byte ch in buffer)
                {
                    count++;
                    if (ch == destBuffer[flagIndex])
                    {
                        flagIndex++;
                    }
                    else
                    {
                        flagIndex = 0;
                    }

                    if (flagIndex >= destBuffer.Length)
                    {
                        index = count;
                        return true;
                    }
                }
            }

            return false;
        }

        public int TotalByteLength
        {
            get
            {
                int count = 0;
                foreach (byte[] item in _listBuffer)
                {
                    count += item.Length;
                }
                return count;
            }
        }
        public byte[] GetAllByte()

        {

            if (_listBuffer.Count == 0)

                return new byte[0];

            if (_listBuffer.Count == 1)

                return _listBuffer[0];

            int byteLen = 0;

            _listBuffer.ForEach(o => byteLen += o.Length);

            byte[] result = new byte[byteLen];

            int index = 0;

            foreach (byte[] item in _listBuffer)

            {

                Buffer.BlockCopy(item, 0, result, index, item.Length);

                index += item.Length;

            }

            return result;

        }
        public byte[] GetSubBuffer(int start, int countTotal)

        {

            if (countTotal == 0)

                return new byte[0];

            byte[] result = new byte[countTotal];

            int countCopyed = 0;

            int indexOfBufferPool = 0;

            foreach (byte[] buffer in _listBuffer)

            {

                //找到起始复制点

                int indexOfItem = 0;

                if (indexOfBufferPool < start)

                {

                    int left = start - indexOfBufferPool;

                    if (buffer.Length <= left)

                    {

                        indexOfBufferPool += buffer.Length;

                        continue;

                    }

                    else

                    {

                        indexOfItem = left;

                        indexOfBufferPool = start;

                    }

                }

                //复制数据

                int dataLeft = buffer.Length - indexOfItem;

                int dataNeed = countTotal - countCopyed;

                if (dataNeed >= dataLeft)

                {

                    Buffer.BlockCopy(buffer, indexOfItem, result, countCopyed, dataLeft);

                    countCopyed += dataLeft;

                }

                else

                {

                    Buffer.BlockCopy(buffer, indexOfItem, result, countCopyed, dataNeed);

                    countCopyed += dataNeed;

                }

                if (countCopyed >= countTotal)

                {

                    Debug.Assert(countCopyed == countTotal);

                    return result;

                }

            }

            throw new Exception("没有足够的数据！");

            // return result;

        }
        //public byte[] GetSubBuffer(int start, int countTotal)
        //{
        //    if (countTotal == 0)
        //        return new byte[0];
        //
        //    byte[] result = new byte[countTotal];
        //    int countCopyed = 0;
        //
        //    int indexOfBufferPool = 0;
        //    foreach (byte[] buffer in _listBuffer)
        //    {
        //        //找到起始复制点
        //        int indexOfItem = 0;
        //        if (indexOfBufferPool < start)
        //        {
        //
        //        }
        //
        //        //复制数据
        //        int dataLeft = buffer.Length - indexOfItem;
        //        int dataNeed = countTotal - countCopyed;
        //        if (dataNeed >= dataLeft)
        //        {
        //            Buffer.BlockCopy(buffer, indexOfItem, result, countCopyed, dataLeft);
        //            countCopyed += dataLeft;
        //        }
        //        else
        //        {
        //            Buffer.BlockCopy(buffer, indexOfItem, result, countCopyed, dataNeed);
        //            countCopyed += dataNeed;
        //        }
        //        if (countCopyed >= countTotal)
        //        {
        //            Debug.Assert(countCopyed == countTotal);
        //            return result;
        //        }
        //    }
        //    throw new Exception("没有足够的数据！");
        //    // return result;
        //}
    }
}
